<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $id
 * @property string $FirstName
 * @property string $LastName
 * @property string $userName
 * @property string $Password
 * @property string $email
 * @property string $Status
 * @property integer $PasswordChangeDate
 * @property integer $lastvisite
 * @property integer $createTime
 *
 * The followings are the available model relations:
 * @property Landlord[] $landlords
 */
class User extends CActiveRecord
{
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		
		return array(
			array('PasswordChangeDate, lastvisite, createTime', 'numerical', 'integerOnly'=>true),
			array('FirstName, LastName, userName, Password', 'length', 'max'=>15),
			array('email', 'length', 'max'=>45),
			array('Status', 'length', 'max'=>8, 'min'=>1, 'message'=>'Select Status'),
			array('userName, Password, FirstName, LastName, email','required'),
			array('userName', 'unique'),
			array('email', 'unique'),
			array('userName', 'length', 'max'=>20, 'min' => 3,'message' => 'Incorrect username (length between 3 and 20 characters).'),
			array('email', 'email'),
			array('userName', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u','message' => 'Incorrect symbols (A-z0-9).'),
			array('Password', 'length', 'min' => 4),
			// array('Status', 'in', 'range' => array( '1','2','3','4' ),'required','message'=>'you forgot to select status.'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, FirstName, LastName, userName, email, Status, lastvisite, createTime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'landlords' => array(self::HAS_ONE, 'Landlord', 'iduser'),
            'tenants' => array(self::HAS_ONE, 'Tenant', 'iduser'),
        );
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'FirstName' => 'First Name',
			'LastName' => 'Last Name',
			'userName' => 'User Name',
			'Password' => 'Password',
			'email' => 'Email',
			'Status' => 'Status',
			'PasswordChangeDate' => 'Password Change Date',
			'lastvisite' => 'Lastvisite',
			'createTime' => 'Create Time',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('FirstName',$this->FirstName,true);
		$criteria->compare('LastName',$this->LastName,true);
		$criteria->compare('userName',$this->userName,true);
		//$criteria->compare('Password',$this->Password,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('Status',$this->Status,true);
		//$criteria->compare('PasswordChangeDate',$this->PasswordChangeDate);
		$criteria->compare('lastvisite',$this->lastvisite);
		$criteria->compare('createTime',$this->createTime);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	/*public function beforeValidate() {
		echo (integer)$this->Status;
	return true;
	
	}*/
	
	/*public function beforeSave()
	{
			
			echo $this->Status;
			
			return false;
	}*/
	
	public function verifyUserName($attribute,$params)
    {
        $username=strtolower($this->userName);
        $user=$this->find('LOWER(username)=?',array($username));
		print_r($user);
        if(!$user===null)
            $this->addError('userName','User Name Already present.');
       /* else if(!$user->validatePassword($this->password))
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
		
		$this->_identity=new UserIdentity($this->username,$this->password);
        if(!$this->_identity->authenticate())
            $this->addError('password','Incorrect username or password.');*/
    }
}